"""
妙趣拾光盒管理系统入口点
这个文件是打包后应用程序的入口点，负责处理可能的依赖问题
"""

import os
import sys
import traceback

def setup_environment():
    """设置运行环境"""
    # 添加当前目录到路径
    current_dir = os.path.dirname(os.path.abspath(__file__))
    if current_dir not in sys.path:
        sys.path.insert(0, current_dir)
    
    # 确保后续导入不会因为缺少模块而完全失败
    try:
        # 尝试应用Prophet补丁
        try:
            from scripts import prophet_patch
            prophet_patch.patch_prophet()
        except Exception as e:
            print(f"警告: 无法应用Prophet补丁: {e}")
            traceback.print_exc()
    except Exception as e:
        print(f"警告: 设置环境时出错: {e}")
        traceback.print_exc()

def main():
    """应用程序主入口"""
    try:
        # 设置环境
        setup_environment()
        
        # 导入应用程序
        from app import create_app, db
        from app.models.users import User
        
        app = create_app()
        
        # 在Flask 2.2+中使用with app.app_context()
        with app.app_context():
            db.create_all()
        
        # 运行应用程序
        app.run(debug=False, host='127.0.0.1', port=5000)
        
    except Exception as e:
        print(f"错误: 应用程序启动失败: {e}")
        traceback.print_exc()
        input("按任意键退出...")
        sys.exit(1)

if __name__ == "__main__":
    main() 